const fileService = require('../services/fileService');

class FileController {
  async uploadFile(req, res, next) {
    try {
      if (!req.file) {
        throw new Error('没有上传文件');
      }

      const fileInfo = await fileService.saveFile(req.file);
      res.status(201).json(fileInfo);
    } catch (error) {
      next(error);
    }
  }

  async deleteFile(req, res, next) {
    try {
      const { fileId } = req.params;
      const result = await fileService.deleteFile(fileId);
      
      if (result) {
        res.json({ message: '文件删除成功' });
      } else {
        res.status(404).json({ message: '文件不存在' });
      }
    } catch (error) {
      next(error);
    }
  }
}

module.exports = new FileController();